﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void main()
        {
            List<int> datasource = new List<int>();
            var parallelQuery = from item in datasource.AsParallel().WithDegreeOfParallelism(4)
                                select item;

            ParallelOptions opt = new ParallelOptions();
            opt.MaxDegreeOfParallelism = 4;

            Parallel.For(0, 100, opt, (i) => Process(i));
            

            List<Student> students = new List<Student>();
            Random ran = new Random();
            for (int i = 0; i < 100; i++)
            {
                students.Add(new Student
                {
                    Name = "学生" + i.ToString(),
                    Score = ran.Next(1, 100)
                });
            }


            int counter = 0;

            var query =

                from student in students.AsParallel()
                where student.Score > 60
                orderby student.Score descending
                select new
                    {
                        TempID = counter++,
                        student.Name,
                        student.Score
                    };


            var query2 = students.AsParallel()
                .Where(student => student.Score > 60)
                .OrderByDescending(stu => stu.Score)
                .AsSequential()
                .Select(studentInfo =>
                new
                {
                    TempID = counter++,
                    studentInfo.Name,
                    studentInfo.Score
                });

            foreach (var item in query.OrderBy(item => item.TempID))
            {
                Console.WriteLine("{0}: 姓名=\"{1}\" 成绩={2}", item.TempID, item.Name, item.Score);
            }
            Console.WriteLine();
            counter = 0;
            foreach (var item in query2)
            {
                Console.WriteLine("{0}: 姓名=\"{1}\" 成绩={2}", item.TempID, item.Name, item.Score);
            }


            Console.ReadKey();



        }

        private static object Process(int i)
        {
            throw new NotImplementedException();
        }




    }

    class Student
    {
        public string Name;
        public int Score;
    }
}
